package com.huobao.yunying.fragments;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.activitys.Act_Dingdan_detail;
import com.huobao.yunying.activitys.Act_Tuikuan_detail;
import com.huobao.yunying.adapters.DingdanManagerAdapter;
import com.huobao.yunying.adapters.TuihuoAdapter;
import com.huobao.yunying.entity.DingdanData;
import com.huobao.yunying.entity.DingdanGood_data;
import com.huobao.yunying.entity.TuihuanHuoList;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.zhangying.R;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class DingdanManagerFragment extends Fragment implements OnScrollListener,OnItemClickListener {
	private DingdanManagerAdapter adapter;
	private TuihuoAdapter tuihuo_adapter;
	private boolean isLoading = true;
	private ListView listview;
	private String type;
	private int m_CurrentPage = 1;
	private TextView tv_foot_msg;
	public DingdanManagerFragment(){}
	/**
	 * [0已取消 11待付款 10已提交 20待发货 30已发货 40已完成]为空则显示全部订单
	 * 
	 * @param type
	 */
//	public DingdanManagerFragment(String type) {
//		this.type = type;
//	}


	public static DingdanManagerFragment getInstant(String type){
		DingdanManagerFragment instance = new DingdanManagerFragment();

		Bundle args = new Bundle();

		args.putString("type", type);


		instance.setArguments(args);

		return instance;
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.test, null);
		this.type = getArguments().getString("type");
		listview = (ListView) view.findViewById(R.id.listview);
		listview.setOnScrollListener(this);
		View FootView = inflater.inflate(R.layout.item_footview, null);
		tv_foot_msg = (TextView) FootView.findViewById(R.id.tv_message);
		listview.addFooterView(FootView);
		adapter = new DingdanManagerAdapter(getActivity(),type);
		tuihuo_adapter = new TuihuoAdapter(getActivity());
		listview.setOnItemClickListener(this);
		if (type == null) {
			listview.setAdapter(tuihuo_adapter);
			getTuihuo(true);
		} else {
			listview.setAdapter(adapter);
			getDate(true);
		}

		return view;
	}

	private void getTuihuo(boolean isShowDialog) {
		isLoading = true;
		Map<String, String> map = HttpUtil.getParams("app", "buyerorder", "act", "refundlist", "page",
				m_CurrentPage + "");
		Http.getInstant().sendRequest(getActivity(), isShowDialog, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				List<TuihuanHuoList> list = JSON.parseArray(jsonObject.getString("retval"), TuihuanHuoList.class);
				if (list == null || list.size() == 0) {
					tv_foot_msg.setText("加载完成");
					isLoading = true;
					return;
				}
				tuihuo_adapter.addAll(list);
				isLoading = false;
			}

			@Override
			public void onHttpError(String error) {
			}
		});
	}

	private void getDate(boolean isShowDialog) {
		isLoading = true;
		Map<String, String> map = HttpUtil.getParams("app", "buyerorder", "status", type, "page", m_CurrentPage + "");
		Http.getInstant().sendRequest(getActivity(), isShowDialog, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				JSONArray array = jsonObject.getJSONArray("retval");
				List<DingdanData> list = new ArrayList<DingdanData>();
				for (int i = 0; i < array.size(); i++) {
					JSONObject object = array.getJSONObject(i);
					DingdanData data = JSON.parseObject(object.toString(), DingdanData.class);
					try {
						List<DingdanGood_data> goods = JSON.parseArray(object.getString("good_data"),
								DingdanGood_data.class);
						data.good_data = goods;
					} catch (Exception e) {
					}
					list.add(data);
				}
				if (list == null || list.size() == 0) {
					tv_foot_msg.setText("加载完成");
					isLoading = true;
					return;
				}
				adapter.addAll(list);
				isLoading = false;
			}

			@Override
			public void onHttpError(String error) {
				// TODO Auto-generated method stub

			}
		});
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		if (isLoading)
			return;
		if ((firstVisibleItem + visibleItemCount) == totalItemCount) {
			m_CurrentPage++;
			if (type == null)
				getTuihuo(false);
			else
				getDate(false);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		
		Intent intent;
		if (type == null){
			if(position == tuihuo_adapter.getCount())return;
			intent = new Intent(getActivity(), Act_Tuikuan_detail.class);
			intent.putExtra("id", tuihuo_adapter.getItem(position).refund_id);
			getActivity().startActivity(intent);
			return;
		}
		if(position == adapter.getCount())return;
		intent = new Intent(getActivity(), Act_Dingdan_detail.class);
		intent.putExtra("order_id", adapter.getItem(position).order_id);
		getActivity().startActivity(intent);
	}

}
